﻿using System.Collections.Concurrent;
using System.Diagnostics;
using System.Threading.Tasks;

namespace ParallelDemo
{
    public class Program
    {
        static void Main(string[] args)
        {
            /*
            Stopwatch stopwatch = Stopwatch.StartNew();
            Function1();
            Function2();
            Console.WriteLine($"串行执行合计耗时{stopwatch.ElapsedMilliseconds}毫秒");
            stopwatch.Restart();

            Console.WriteLine("************");

            Parallel.Invoke(Function1, Function2);
            stopwatch.Stop();
            Console.WriteLine($"串行执行合计耗时{stopwatch.ElapsedMilliseconds}毫秒");
            Console.ReadKey();
            */

            /*
            for (int j = 1; j < 4; j++)
            {
                Console.WriteLine($"第{j}次比较");
                ConcurrentBag<int> bags = new ConcurrentBag<int>();
                var stopwatch = Stopwatch.StartNew();
                for (int i = 0; i < 10000000; i++)
                {
                    bags.Add(i);
                }
                Console.WriteLine("串行计算：集合有:{0},总共耗时：{1}毫秒", bags.Count, stopwatch.ElapsedMilliseconds);

                bags = new ConcurrentBag<int>();
                stopwatch.Restart();
                Parallel.For(0, 10000000, i =>
                {
                    bags.Add(i);
                });
                Console.WriteLine("并行计算：集合有:{0},总共耗时：{1}毫秒", bags.Count, stopwatch.ElapsedMilliseconds);
                Console.WriteLine();
            }
            Console.ReadKey();
            */

            /*
            for (int j = 1; j < 4; j++)
            {
                Console.WriteLine($"第{j}次比较");
                ConcurrentBag<int> bag = new ConcurrentBag<int>();
                var watch = Stopwatch.StartNew();
                for (int i = 0; i < 3000000; i++)
                {
                    bag.Add(i);
                }
                Console.WriteLine("串行计算：集合有:{0},总共耗时：{1}", bag.Count, watch.ElapsedMilliseconds);

                bag = new ConcurrentBag<int>();
                watch.Restart();
                Parallel.ForEach(Partitioner.Create(0, 3000000), i =>
                {
                    for (int m = i.Item1; m < i.Item2; m++)
                    {
                        bag.Add(m);
                    }
                });
                Console.WriteLine("并行计算：集合有:{0},总共耗时：{1}", bag.Count, watch.ElapsedMilliseconds);
            }
            */

            /*
            var watch = Stopwatch.StartNew();
            ConcurrentBag<int> bag = new ConcurrentBag<int>();
            Parallel.For(0, 20000000, (i, state) =>
            {
                if (bag.Count == 1000)
                {
                    state.Break();
                    return;
                }
                bag.Add(i);
            });
            Console.WriteLine("当前集合有{0}个元素。", bag.Count);
            */


            /*
            try
            {
                Parallel.Invoke(Function1, Function2);
            }
            catch (AggregateException ex)
            {
                foreach (var single in ex.InnerExceptions)
                {
                    Console.WriteLine(single.Message);
                }
            }

            Console.Read();
            */

            var bag = new ConcurrentBag<int>();
            ParallelOptions options = new ParallelOptions();
            //指定使用的硬件线程数为1
            options.MaxDegreeOfParallelism = 1;
            Parallel.For(0, 300000, options, i =>
            {
                bag.Add(i);
            });

            Console.WriteLine("并行计算：集合有:{0}", bag.Count);
        }
        static void Function1()
        {
            Console.WriteLine("方法一运行3秒");
            Thread.Sleep(3000);
            //throw new Exception("方法一的异常");
        }
        static void Function2()
        {
            Console.WriteLine("方法二运行5秒");
            Thread.Sleep(5000);
            //throw new Exception("方法二的异常");
        }
    }
}

